<template>
    <div id="className" :style="{ height: height, width: width }"></div>
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '350px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            data: [{name:"1999-6-1",value:["1999-6-1",111]}],
            now: new Date(1997, 9, 3, 12, 12, 12),
            oneDay: 24 * 3600 * 1000,
            value: Math.random() * 1000,
            chart: null
        };
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        }
    },
    mounted() {
        this.chart = echarts.init(this.$el, 'macarons')
        //this.setOptions(this.chartData)
        //this.generateData();
        this.renderChart();
        //setInterval(this.updateData, 1000);
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.chartData)
        },
        randomData() {
            let randomNumber = Math.floor(Math.random() * 6000);
            this.now = new Date(+this.now + this.oneDay+randomNumber);
            this.value = this.value + Math.random() * 21 - 10;
            let aa=  [this.now.getFullYear(), this.now.getMonth() + 1, this.now.getDate()].join('-')+" "+this.now.getHours()+ ":"+this.now.getMinutes()+":"+ this.now.getSeconds()
            console.log(aa)
            return {
                name: aa,
                value: [
                aa,
                    Math.round(this.value)
                ]
            };
        },
        generateData() {
            for (let i = 0; i < 1000; i++) {
                this.data.push(this.randomData());
            }
        },
        updateData() {
            for (let i = 0; i < 5; i++) {
                this.data.shift();
                this.data.push(this.randomData());
            }
            this.chart.setOption({
                series: [
                    {
                        data: this.data
                    }
                ]
            });
        },
        renderChart() {
            const option = {
                title: {
                    text: 'Dynamic',
                    x: 'center'
                }, grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: (params) => {
                        const date = new Date(params[0].name);
                        return (
                           /*  date.getFullYear() + '-' +
                            (date.getMonth() + 1) + '-' +
                            date.getDate() + ' ' +
                            date.getHours() + ':' +
                            date.getMinutes() + ':' +
                            date.getSeconds() + */
                            params[0].name +
                            ' : ' +
                            params[0].value[1]
                        );
                    },
                    axisPointer: {
                        animation: false
                    }
                },
                xAxis: {
                    type: 'time',
                    splitLine: {
                        show: false
                    }
                },
                yAxis: {
                    type: 'value',
                    boundaryGap: [0, '50%'],
                    splitLine: {
                        show: true
                    }
                },
                legend: {
                    data: ['Fake Data',],
                    bottom: "1"
                },
                series: [
                    {
                        name: 'Fake Data',
                        type: 'line',
                        showSymbol: false,
                        smooth: true,
                        data: this.data
                    }
                ]
            };
            this.chart.setOption(option);
        }
    }
};
</script>

<style>
/* Add your custom styles here */
</style>